Attribute VB_Name = "modDP5_Protocol"
Option Explicit

'============================================================================
' 20180620 - Updates for Mini-X2
'============================================================================

Public Const ACK = 0
Public Const SYNC1_ = &HF5
Public Const SYNC2_ = &HFA

Public Enum TRANSMIT_PACKET_TYPE
     'REQUEST_PACKETS_TO_DP5
    XMTPT_SEND_STATUS
    XMTPT_SEND_SPECTRUM
    XMTPT_SEND_SPECTRUM_STATUS
    XMTPT_SEND_CLEAR_SPECTRUM
    XMTPT_SEND_CLEAR_SPECTRUM_STATUS
    XMTPT_BUFFER_SPECTRUM
    XMTPT_BUFFER_CLEAR_SPECTRUM
    XMTPT_SEND_BUFFER
    XMTPT_SEND_SCOPE_DATA
    XMTPT_SEND_512_BYTE_MISC_DATA
    XMTPT_SEND_SCOPE_DATA_REARM
    XMTPT_SEND_ETHERNET_SETTINGS
    XMTPT_SEND_DIAGNOSTIC_DATA
    XMTPT_SEND_NETFINDER_PACKET
    XMTPT_SEND_HARDWARE_DESCRIPTION
    XMTPT_SEND_SCA
    XMTPT_LATCH_SEND_SCA
    XMTPT_LATCH_CLEAR_SEND_SCA
    XMTPT_SEND_ROI_OR_FIXED_BLOCK
    XMTPT_TEXT_CONFIGURATION                'sends configuration commands without any changes
    XMTPT_TEXT_CONFIGURATION_MX2            'Mini-X2 configuration commands
    XMTPT_TEXT_CONFIGURATION_FILTERED       'runs filters/fixes then sends configuration commands
    XMTPT_READ_TEXT_CONFIGURATION
    XMTPT_READ_TEXT_CONFIGURATION_MX2
    XMTPT_FULL_READ_TEXT_CONFIGURATION
    XMTPT_ERASE_FPGA_IMAGE
    XMTPT_UPLOAD_PACKET_FPGA
    XMTPT_REINITIALIZE_FPGA
    XMTPT_ERASE_UC_IMAGE_0
    XMTPT_ERASE_UC_IMAGE_1
    XMTPT_ERASE_UC_IMAGE_2
    XMTPT_UPLOAD_PACKET_UC
    XMTPT_SWITCH_TO_UC_IMAGE_0
    XMTPT_SWITCH_TO_UC_IMAGE_1
    XMTPT_SWITCH_TO_UC_IMAGE_2
    XMTPT_UC_FPGA_CHECKSUMS

    'VENDOR_REQUESTS_TO_DP5
    XMTPT_CLEAR_SPECTRUM_BUFFER
    XMTPT_ENABLE_MCA_MCS
    XMTPT_DISABLE_MCA_MCS
    XMTPT_ARM_DIGITAL_OSCILLOSCOPE
    XMTPT_AUTOSET_INPUT_OFFSET
    XMTPT_AUTOSET_FAST_THRESHOLD
    XMTPT_READ_IO3_0
    XMTPT_WRITE_IO3_0
    XMTPT_WRITE_512_BYTE_MISC_DATA
    XMTPT_SET_DCAL
    XMTPT_SET_PZ_CORRECTION_UC_TEMP_CAL_PZ
    XMTPT_SET_PZ_CORRECTION_UC_TEMP_CAL_UC
    XMTPT_SET_BOOT_FLAGS
    XMTPT_SET_HV_DP4_EMULATION
    XMTPT_SET_TEC_DP4_EMULATION
    XMTPT_SET_INPUT_OFFSET_DP4_EMULATION
    XMTPT_SET_ADC_CAL_GAIN_OFFSET
    XMTPT_SET_SPECTRUM_OFFSET
    XMTPT_REQ_SCOPE_DATA_MISC_DATA_SCA_PACKETS
    XMTPT_SET_SERIAL_NUMBER
    XMTPT_CLEAR_GP_COUNTER
    XMTPT_SWITCH_SUPPLIES
    
    XMTPT_SEND_TEST_PACKET
    
    'REQUEST_PACKETS_TO_MX2
    XMTPT_SEND_TUBE_ILOCK_TABLE_MX2
    XMTPT_SEND_WARMUP_TABLE_MX2
    XMTPT_SEND_TIMESTAMP_RECORD_MX2
    XMTPT_SEND_FAULT_RECORD_MX2
    
    'REQUEST_PACKETS_TO_MX2 with simple data
    XMTPT_WRITE_TIMESTAMP_MX2
    XMTPT_INITIATE_WARMUP_DAILY_SEQUENCE_MX2
    XMTPT_INITIATE_WARMUP_MONTHLY_SEQUENCE_MX2
    
    'REQUEST_PACKETS_TO_MX2 by Firmware Manager
    XMTPT_WRITE_TUBE_ILOCK_TABLE_MX2
    XMTPT_WRITE_WARMUP_SEQUENCE_MX2
    
End Enum  'TRANSMIT_PACKET_TYPE

Public Enum PID1_TYPE
    PID1_REQ_STATUS = &H1
    PID1_REQ_SPECTRUM = &H2
    PID1_REQ_SCOPE_MISC = &H3
    PID1_REQ_SCA = &H4
    PID1_REQ_CONFIG = &H20
    PID1_REQ_FPGA_UC = &H30
    PID1_RCV_STATUS = &H80
    PID1_RCV_SPECTRUM = &H81
    PID1_RCV_SCOPE_MISC = &H82
    PID1_RCV_SCA = &H83
    PID1_RCV_ECHO = &H8F
    PID1_VENDOR_REQ = &HF0
    PID1_COMM_TEST = &HF1
    PID1_ACK = &HFF
End Enum  'PID1_TYPE

Public Enum PID2_REQ_STATUS_TYPE
    PID2_SEND_STATUS = &H1
End Enum  'PID2_REQ_STATUS_TYPE

Public Enum PID2_REQ_SPECTRUM_TYPE
    ' REQUEST_PACKETS_TO_DP5
    PID2_SEND_SPECTRUM = &H1
    PID2_SEND_CLEAR_SPECTRUM = &H2
    PID2_SEND_SPECTRUM_STATUS = &H3
    PID2_SEND_CLEAR_SPECTRUM_STATUS = &H4
    'PID2_BUFFER_SPECTRUM
    'PID2_BUFFER_CLEAR_SPECTRUM
    'PID2_SEND_BUFFER
    'PID2_SEND_CONFIG
End Enum  'PID2_REQ_SPECTRUM_TYPE

Public Enum PID2_REQ_SCOPE_MISC_TYPE    'PID1_REQ_SCOPE_MISC = &H3
    PID2_SEND_SCOPE_DATA = &H1
    PID2_SEND_512_BYTE_MISC_DATA = &H2
    PID2_SEND_SCOPE_DATA_REARM = &H3
    PID2_SEND_ETHERNET_SETTINGS = &H4
    PID2_SEND_DIAGNOSTIC_DATA = &H5
    PID2_SEND_HARDWARE_DESCRIPTION = &H6
    PID2_SEND_NETFINDER_READBACK = &H7
    PID2_SEND_I2C_TRANSFER = &H8
    
    '--------------------------------------------------------------
    PID2_SEND_MX2_TUBE_ILOCK_TABLE = &HB    'Mini-X2 Only Functions
    PID2_SEND_MX2_WARMUP_TABLE = &HC
    PID2_SEND_MX2_TIMESTAMP_RECORD = &HD
    PID2_SEND_MX2_FAULT_RECORD = &HE
End Enum  'PID2_REQ_SCOPE_MISC_TYPE

Public Enum PID2_REQ_SCA_TYPE
    PID2_SEND_SCA = &H1
    PID2_LATCH_SEND_SCA = &H2
    PID2_LATCH_CLEAR_SEND_SCA = &H3
End Enum  'PID2_REQ_SCA_TYPE

Public Enum PID2_REQ_CONFIG_TYPE            'PID1_REQ_CONFIG = &H20
    PID2_TEXT_CONFIG_PACKET = &H2
    PID2_CONFIG_READBACK_PACKET = &H3
End Enum  'PID2_SEND_CONFIG_TYPE

Public Enum PID2_REQ_FPGA_UC_TYPE
    PID2_ERASE_FPGA_IMAGE = &H1
    PID2_UPLOAD_PACKET_FPGA = &H2
    PID2_REINITIALIZE_FPGA = &H3
    PID2_ERASE_UC_IMAGE_0 = &H4
    PID2_ERASE_UC_IMAGE_1 = &H5
    PID2_ERASE_UC_IMAGE_2 = &H6
    PID2_UPLOAD_PACKET_UC = &H7
    PID2_SWITCH_TO_UC_IMAGE_0 = &H8
    PID2_SWITCH_TO_UC_IMAGE_1 = &H9
    PID2_SWITCH_TO_UC_IMAGE_2 = &HA
    'PID2_UC_FPGA_CHECKSUMS
End Enum  'PID2_REQ_FPGA_UC_TYPE

Public Enum PID2_VENDOR_REQ_TYPE            'PID1_VENDOR_REQ = &HF0
    PID2_CLEAR_SPECTRUM = &H1
    PID2_ENABLE_MCA_MCS = &H2
    PID2_DISABLE_MCA_MCS = &H3
    PID2_ARM_DIGITAL_OSCILLOSCOPE = &H4
    PID2_AUTOSET_INPUT_OFFSET = &H5
    PID2_AUTOSET_FAST_THRESHOLD = &H6
    PID2_READ_IO3_0 = &H7
    PID2_WRITE_IO3_0 = &H8
    PID2_WRITE_512_BYTE_MISC_DATA = &H9
    PID2_SET_DCAL = &HA
    PID2_SET_PZ_CORRECTION_UC_TEMP_CAL_PZ = &HB
    PID2_SET_PZ_CORRECTION_UC_TEMP_CAL_UC = &HC
    PID2_SET_BOOT_FLAGS = &HD
    'PID2_SET_HV_DP4_EMULATION
    'PID2_SET_TEC_DP4_EMULATION
    'PID2_SET_INPUT_OFFSET_DP4_EMULATION
    PID2_SET_ADC_CAL_GAIN_OFFSET = &HE
    'PID2_SET_SPECTRUM_OFFSET
    'PID2_REQ_SCOPE_DATA_MISC_DATA_SCA_PACKETS
    PID2_SET_SERIAL_NUMBER = &HF
    PID2_CLEAR_GP_COUNTER = &H10
    PID2_SET_ETHERNET_SETTINGS = &H11
    PID2_SET_BAUD_RATE = &H13
    'PID2_SWITCH_SUPPLIES
    PID2_ETHERNET_ALLOW_SHAREING = &H20
    PID2_ETHERNET_NO_SHARING = &H21
    PID2_ETHERNET_LOCK_IP = &H22
    
    '----------------------------------------------------------------
    PID2_WRITE_MX2_TUBE_ILOCK_TABLE = &H30    'Mini-X2 Only Functions
    PID2_WRITE_MX2_TIMESTAMP = &H31
    PID2_WRITE_MX2_WARMUP_SEQUENCE = &H32
    PID2_INIT_MX2_WARMUP_SEQUENCE = &H33
End Enum  'PID2_VENDOR_REQ_TYPE

Public Enum PID2_REQ_COMM_TEST                  'PID1_COMM_TEST = &HF1
    PID2_REQ_COMM_ACK_OK = &H0
    PID2_REQ_COMM_ACK_SYNC_ERROR = &H1
    PID2_REQ_COMM_ACK_PID_ERROR = &H2
    PID2_REQ_COMM_ACK_LEN_ERROR = &H3
    PID2_REQ_COMM_ACK_CHECKSUM_ERROR = &H4
    PID2_REQ_COMM_ACK_BAD_PARAM = &H5
    PID2_REQ_COMM_ACK_BAD_HEX_REC = &H6
    PID2_REQ_COMM_ACK_UNRECOG = &H7
    PID2_REQ_COMM_ACK_FPGA_ERROR = &H8
    PID2_REQ_COMM_ACK_CP2201_NOT_FOUND = &H9
    PID2_REQ_COMM_ACK_SCOPE_DATA_NOT_AVAIL = &HA
    PID2_REQ_COMM_ACK_PC5_NOT_PRESENT = &HB
    PID2_REQ_COMM_ACK_OK_ETHERNET_SHARE_REQ = &HC
    PID2_REQ_COMM_ACK_ETHERNET_BUSY = &HD
    PID2_REQ_COMM_ACK_I2C_ERROR = &HE
    PID2_REQ_COMM_ACK_OK_FPGA_ADDRESS = &HF
    PID2_REQ_COMM_ECHO_PACKET = &H7F
End Enum 'PID2_REQ_COMM_TEST

Public Enum PID2_RCV_STATUS_TYPE                'PID1_RCV_STATUS = &H80
    RCVPT_DPP_STATUS = &H1
    RCVPT_MX2_STATUS = &H2
End Enum  'PID2_RCV_STATUS_TYPE
    
Public Enum PID2_RCV_SPECTRUM_TYPE
    RCVPT_256_CHANNEL_SPECTRUM = &H1
    RCVPT_256_CHANNEL_SPECTRUM_STATUS = &H2
    RCVPT_512_CHANNEL_SPECTRUM = &H3
    RCVPT_512_CHANNEL_SPECTRUM_STATUS = &H4
    RCVPT_1024_CHANNEL_SPECTRUM = &H5
    RCVPT_1024_CHANNEL_SPECTRUM_STATUS = &H6
    RCVPT_2048_CHANNEL_SPECTRUM = &H7
    RCVPT_2048_CHANNEL_SPECTRUM_STATUS = &H8
    RCVPT_4096_CHANNEL_SPECTRUM = &H9
    RCVPT_4096_CHANNEL_SPECTRUM_STATUS = &HA
    RCVPT_8192_CHANNEL_SPECTRUM = &HB
    RCVPT_8192_CHANNEL_SPECTRUM_STATUS = &HC
End Enum  'PID2_RCV_SPECTRUM_TYPE

Public Enum PID2_RCV_SCOPE_MISC_TYPE
    RCVPT_SCOPE_DATA = &H1
    RCVPT_512_BYTE_MISC_DATA = &H2
    RCVPT_SCOPE_DATA_WITH_OVERFLOW = &H3
    RCVPT_ETHERNET_SETTINGS = &H4
    RCVPT_DIAGNOSTIC_DATA = &H5
    RCVPT_HARDWARE_DESCRIPTION = &H6
    RCVPT_CONFIG_READBACK = &H7
    RCVPT_NETFINDER_READBACK = &H8
    
    '--------------------------------------------------------------
    RCVPT_MX2_TUBE_ILOCK_TABLE = &HD        'Mini-X2 Only Functions
    RCVPT_MX2_WARMUP_TABLE = &HE
    RCVPT_MX2_TIMESTAMP_RECORD = &HF
    RCVPT_MX2_FAULT_RECORD = &H10
End Enum  'PID2_REQ_SCOPE_MISC_TYPE

Public Enum PID2_RCV_SCA_TYPE
    RCVPT_SCA = &H1
End Enum  'PID2_REQ_SCA_TYPE

Public Enum PID2_ACK_TYPE  'PID1_ACK, PID2_ACK_TYPE)
    PID2_ACK_OK = &H0
    PID2_ACK_SYNC_ERROR = &H1
    PID2_ACK_PID_ERROR = &H2
    PID2_ACK_LEN_ERROR = &H3
    PID2_ACK_CHECKSUM_ERROR = &H4
    PID2_ACK_BAD_PARAM = &H5
    PID2_ACK_BAD_HEX_REC = &H6
    PID2_ACK_UNRECOG = &H7
    PID2_ACK_FPGA_ERROR = &H8
    PID2_ACK_CP2201_NOT_FOUND = &H9
    PID2_ACK_SCOPE_DATA_NOT_AVAIL = &HA
    PID2_ACK_PC5_NOT_PRESENT = &HB
    PID2_ACK_OK_ETHERNET_SHARE_REQ = &HC
    PID2_ACK_ETHERNET_BUSY = &HD
    PID2_ACK_I2C_ERROR = &HE
    PID2_ACK_OK_FPGA_ADDRESS = &HF
    PID2_ACK_NO_FPGA_SUPPORT = &H10
    PID2_ACK_NO_CALIBRATION_DATA = &H11
End Enum  'PID2_ACK_TYPE

Public Enum PX5_OPTIONS
    PX5_OPTION_NONE = 0
    PX5_OPTION_HPGe_HVPS = 1
    PX5_OPTION_TEST_TEK = 4
    PX5_OPTION_TEST_MOX = 5
    PX5_OPTION_TEST_AMP = 6
    PX5_OPTION_TEST_MODE_1 = 8
    PX5_OPTION_TEST_MODE_2 = 9
End Enum

Public Type Stat
    RAW(64) As Byte
    SerialNumber As Long
    FastCount As Double
    SlowCount As Double
    FPGA As Byte
    Firmware As Byte
    Build As Byte
    AccumulationTime As Double
    HV As Double
    DET_TEMP As Double
    DP5_TEMP As Double
    PX4 As Boolean
    AFAST_LOCKED As Boolean
    MCA_EN As Boolean
    PresetCountDone As Boolean
    PresetRtDone As Boolean
    PresetLtDone As Boolean
    SUPPLIES_ON As Boolean
    SCOPE_DR As Boolean
    DP5_CONFIGURED As Boolean
    GP_COUNTER As Double
    AOFFSET_LOCKED As Boolean
    MCS_DONE As Boolean
    DCAL As Double
    PZCORR As Byte  ' or single?
    UC_TEMP_OFFSET As Byte
    AN_IN As Double
    VREF_IN As Double
    PC5_SerialNumber As Long
    PC5_PRESENT As Boolean
    PC5_HV_POL As Boolean
    PC5_8_5V As Boolean
    LiveTime As Double
    RealTime As Double
    BOOT_FLAG_LSB As Byte
    BOOT_FLAG_MSB As Byte
    BOOT_HV As Double
    BOOT_TEC As Double
    BOOT_INPOFFSET As Double    ' or long?
    ADC_GAIN_CAL As Double
    ADC_OFFSET_CAL As Byte
    SPECTRUM_OFFSET As Long     ' or single?
   
    b80MHzMode As Boolean
    bFPGAAutoClock As Boolean
    DEVICE_ID As Byte           ' 0=DP5, 1=PX5, 2=DP5G, 3=MCA8000D, 4=TB5
    strDeviceID As String
    ReBootFlag As Boolean       ' FW6.05 and later
    DPP_options As Byte         ' 1=HPGe HVPS
    HPGe_HV_INH As Boolean
    HPGe_HV_INH_POL As Boolean
    TEC_Voltage As Double
    DPP_ECO As Byte
    AU34_2 As Boolean
    isAscInstalled As Boolean
    isAscEnabled As Boolean
    bScintHas80MHzOption As Boolean
    isDP5_RevDxGains As Boolean
End Type

Public STATUS As Stat

